package com.tusuo.stds.ui.suggest;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.tusuo.stds.R;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.util.PicDeleteDialog;

import java.util.ArrayList;
import java.util.List;

public class PictrueDetailActivity extends BaseToolbarActivity<EmptyView,EmptyPresenter> implements View.OnClickListener{

    ViewPager vpPic;

    ArrayList<ImageView> images;
    private ArrayList<String> picList;
    private int currentIndex = 1;
    private ImageView ivDelete;
    private MyPagerAdapter adapter;
    private PicDeleteDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pictrue_detail);
        vpPic = (ViewPager) findViewById(R.id.pic_detail_vp);
        ivDelete = (ImageView) findViewById(R.id.pic_detail_iv_delete);
        images = new ArrayList<>();

        ivDelete.setOnClickListener(this);

        initData();

        adapter = new MyPagerAdapter();

        vpPic.setAdapter(adapter);

        vpPic.setCurrentItem(currentIndex - 1);

        vpPic.setOffscreenPageLimit(0);

        vpPic.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                switch (state) {
                    case 0: // 什么都没做

                        break;

                    case 1: // 正在滑动

                        break;

                    case 2: // 滑动完成
                        setConfig(concatConfig(vpPic.getCurrentItem() + 1),null);
                        break;
                }
            }
        });
    }

    @NonNull
    @Override
    public EmptyPresenter createPresenter() {
        return new EmptyPresenter();
    }

    private void initData() {
        setupUI(this,getResources().getColor(R.color.white),getResources().getColor(R.color.black));
        Intent intent = getIntent();
        if (intent != null) {
            picList = intent.getStringArrayListExtra("imagePath");
            currentIndex = intent.getIntExtra("currentImage", 1);
        }

        if (picList.size() != 0) {
            for (String picPath : picList) {
                ImageView image = new ImageView(this);
                Glide
                        .with(this)
                        .load(picPath)
                        .centerCrop()
                        .into(image);
                images.add(image);
            }
        }


        setConfig(concatConfig(currentIndex),null);

        Intent intent1 = new Intent();
        intent1.putExtra("imagePath",picList);
        setResult(10024,intent1);
    }

    public String concatConfig(int index) {
        String config = index + "/" + images.size();
        return config;
    }

    public void setConfig(String configStr, List<ToolbarOwner.MenuAction> menuActionList) {
        ToolbarOwner.Config config = new ToolbarOwner.Config(configStr, true, R.drawable.ic_back_dark, menuActionList);
        mToolbarOwner.setConfig(config);

    }

    @Override
    public void onClick(View v) {

        if (v.getId() == R.id.pic_detail_iv_delete) {
            showDialog();
        }

    }

    public void showDialog() {

        dialog = new PicDeleteDialog(this);

        dialog.setOnDeleteClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                images.remove(vpPic.getCurrentItem());
                picList.remove(vpPic.getCurrentItem());
                adapter.notifyDataSetChanged();
                if (images.size() == 0) {
                    setConfig(concatConfig(0),null);
                    dialog.dismiss();
                    finish();
                }else {
                    setConfig(concatConfig(vpPic.getCurrentItem() + 1),null);
                    dialog.dismiss();
                }
            }
        });

        dialog.show();
    }


    private class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            if (images.size() == 0) {
                return 0;
            }else {
                return images.size();
            }
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(images.get(position));
            return images.get(position);
        }


        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
